<template>
  <div class="app-container">
    <el-row>
      <!--区域数据-->
      <el-col :xs="24" v-show="treeVisibale === true" class="tree_bg">
        <!-- <div>
          <el-input
            v-model="keyWordName"
            placeholder="输入关键字进行过滤"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div> -->
        <div class="head-container">
          <el-tree
            :data="areaOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
            v-loading="treeloading"
          />
        </div>
        <div class="tree_close" @click="changeTreeShow">
          <i class="el-icon-caret-left" style="color: white"></i>
        </div>
      </el-col>
      <el-col class="jiange"></el-col>
      <div class="tree_open" @click="changeTreeShow" v-show="treeVisibale === false">
        <i class="el-icon-caret-right" style="color: white"></i>
      </div>
      <el-col :xs="24" :class="treeVisibale ? 'table_small_bg' : 'table_big_bg'">
        <el-row class="line-bottom" v-show="showSearch">
          <el-col class="form_col">
           <el-form :model="queryParams" ref="queryForm" :size="size" v-show="showSearch" :label-width="labelWidth">
            <div id="searchFilter" :gutter="10" style="display: flex; flex-wrap: wrap">
              <el-form-item label="设备编号" prop="TerminalID">
                <el-input
                  v-model="queryParams.TerminalID"
                  placeholder="请输入设备编号"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="时间选择" prop="FeezeTime">
                <!-- <el-date-picker
                  v-model="queryParams.FeezeTime"
                  type="monthrange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM"
                  style="width: 120%;">
                </el-date-picker> -->
                <el-date-picker
                    v-model="queryParams.FeezeTime"
                    type="month"
                    value-format="yyyy-MM"
                    placeholder="选择日期"
                    style="width: 120%;">
                  </el-date-picker>
                </el-form-item>
              </el-form-item>

            </div>
          </el-form>
          </el-col>
          <el-col class="form_query">
            <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
            <el-button @click="resetQuery" size="mini">重置</el-button>
          </el-col>
        </el-row>


        <el-row :gutter="10" class="mb8 padding_top_bottom">

          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['datastatistics:terminalsmonfreezedb:export']"
            >导出</el-button>
          </el-col> -->

          <RightDropdown :columns="columns" :showSearch.sync="showSearch" @queryTable="getList" @export="handleExport" :hasPermiExport="['datastatistics:terminalsmonfreezedb:export']"></RightDropdown>
        </el-row>

        <el-table ref="table" v-loading="loading" :data="terminalsmonfreezedbList" @selection-change="handleSelectionChange"  class="table-h">
          <el-table-column type="selection" width="55" align="center" fixed />
<!--          <el-table-column label="主键" align="center" prop="Id"  v-if="columns[0].visible" :show-overflow-tooltip="true" />-->
<!--          <el-table-column label="设备归属水司" align="center" prop="tenantId" v-if="columns[1].visible" :show-overflow-tooltip="true" width="120"/>-->
<!--          <el-table-column label="用户主键" align="center" prop="UserID" v-if="columns[2].visible" :show-overflow-tooltip="true" />-->


<!--          <el-table-column label="位置识别编号" align="center" prop="locationNb" v-if="columns[4].visible" :show-overflow-tooltip="true"  width="120"/>-->
<!--          <el-table-column label="冻结月份" align="center" prop="FeezeTimeN" width="180" v-if="columns[28].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.FeezeTimeN, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>-->
          <el-table-column label="冻结月份" align="center" prop="FeezeTime" v-if="columns[0].visible" :show-overflow-tooltip="true" width="150"/>
          <el-table-column label="设备名称" align="center" prop="TerminalName" v-if="columns[1].visible" :show-overflow-tooltip="true" width="150"/>
          <el-table-column label="设备编号" align="center" prop="TerminalID" v-if="columns[2].visible" :show-overflow-tooltip="true" width="150"/>
          <el-table-column label="正向月用量(m³)" align="center" prop="ReadDosageN" v-if="columns[3].visible" :show-overflow-tooltip="true" width="150" />
          <el-table-column label="反向月用量(m³)" align="center" prop="ReverseDosageN" v-if="columns[4].visible" :show-overflow-tooltip="true" width="150"/>
          <el-table-column label="净用量(m³)" align="center" prop="DosageN" v-if="columns[5].visible" :show-overflow-tooltip="true" width="140"/>
<!--          <el-table-column label="设备地址" align="center" prop="TerminalAddress" v-if="columns[6].visible" :show-overflow-tooltip="true" />-->
<!--          <el-table-column label="上次累计读数" align="center" prop="ReadNumberO" v-if="columns[7].visible" :show-overflow-tooltip="true" width="120"/>-->
          <el-table-column label="累计正向读数(m³)" align="center" prop="ReadNumberN" v-if="columns[6].visible" :show-overflow-tooltip="true" width="160"/>
          <el-table-column label="累计反向读数(m³)" align="center" prop="ReverseNumberN" v-if="columns[7].visible" :show-overflow-tooltip="true" width="160" />
          <el-table-column label="总流量(m³)" align="center" prop="TotalFlowN" v-if="columns[8].visible" :show-overflow-tooltip="true" width="140" />
<!--          <el-table-column label="上次累计反向读数" align="center" prop="ReverseNumberO" v-if="columns[9].visible" :show-overflow-tooltip="true" width="160"/>-->

<!--          <el-table-column label="上期冻结日的总流量" align="center" prop="TotalFlowO" v-if="columns[11].visible" :show-overflow-tooltip="true" width="180" />-->


<!--          <el-table-column label="上期正向日用量" align="center" prop="ReadDosageO" v-if="columns[14].visible" :show-overflow-tooltip="true" width="120"/>-->
<!--          <el-table-column label="上次反向日用量" align="center" prop="ReverseDosageO" v-if="columns[15].visible" :show-overflow-tooltip="true" width="120"/>-->

<!--          <el-table-column label="上次净用量" align="center" prop="DosageO" v-if="columns[18].visible" :show-overflow-tooltip="true" width="120"/>-->
<!--          <el-table-column label="累计读数增长率" align="center" prop="ReadGrowthRate" v-if="columns[19].visible" :show-overflow-tooltip="true" width="140"/>-->
<!--          <el-table-column label="反向累计读数增长率" align="center" prop="ReverseGrowthRate" v-if="columns[20].visible" :show-overflow-tooltip="true" width="180"/>-->
<!--          <el-table-column label="累计读数增长率" align="center" prop="TotalFlowGrowthRate" v-if="columns[21].visible" :show-overflow-tooltip="true" width="140"/>-->
<!--          <el-table-column label="用量增长比率" align="center" prop="ReadDosageGrowthRate" v-if="columns[22].visible" :show-overflow-tooltip="true" width="120"/>-->
<!--          <el-table-column label="反向用量增长比率" align="center" prop="ReverseDosageGrowthRate" v-if="columns[23].visible" :show-overflow-tooltip="true" width="180"/>-->
<!--          <el-table-column label="用量增长比率" align="center" prop="DosageGrowthRate" v-if="columns[24].visible" :show-overflow-tooltip="true" width="120"/>-->
          <!-- <el-table-column label="创建时间" align="center" prop="CreateTime" width="180" v-if="columns[25].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.CreateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column> -->
<!--          <el-table-column label="上次冻结时间" align="center" prop="GetTimeO" width="180" v-if="columns[26].visible" :show-overflow-tooltip="true">-->
<!--            <template slot-scope="scope">-->
<!--              <span>{{ parseTime(scope.row.GetTimeO, '{y}-{m}-{d}') }}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="数据生成时间" align="center" prop="GetTimeN" width="180" v-if="columns[27].visible" :show-overflow-tooltip="true">-->
<!--            <template slot-scope="scope">-->
<!--              <span>{{ parseTime(scope.row.GetTimeN, '{y}-{m}-{d}') }}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->

<!--          <el-table-column label="上次月冻结的月份" align="center" prop="FeezeTimeO" width="180" v-if="columns[29].visible" :show-overflow-tooltip="true">-->
<!--            <template slot-scope="scope">-->
<!--              <span>{{ parseTime(scope.row.FeezeTimeO, '{y}-{m}-{d}') }}</span>-->
<!--            </template>-->
<!--          </el-table-column>-->

        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import { listTerminalsmonfreezedb, getTerminalsmonfreezedb, delTerminalsmonfreezedb, addTerminalsmonfreezedb, updateTerminalsmonfreezedb } from "@/api/dataquery/terminal/terminalsmonfreezedb";
// import { getAreasList } from '@/api/basedata/areas'
import moment from 'moment'

export default {
  name: "Terminalsmonfreezedb",
  data() {
    return {
      selectOptions:{},
      size:'small',
      collapseAble: false,
      fold: false,
      maxShow: '3',
      labelWidth: '110px',
      // 列信息
      columns: [
        { key: 0, label: `冻结月份`, visible: true },
        { key: 1, label: `设备名称`, visible: true },
        { key: 2, label: `设备编号`, visible: true },
        { key: 3, label: `正向日用量`, visible: true },
        { key: 4, label: `反向日用量`, visible: true },
        { key: 5, label: `净用量`, visible: true },
        { key: 6, label: `累计正向读数`, visible: true },
        { key: 7, label: `累计反向读数`, visible: true },
        { key: 8, label: `总流量`, visible: true },
        // { key: 8, label: `本次累计读数`, visible: false },
        // { key: 9, label: `上次累计反向读数`, visible: true },
        // { key: 10, label: `本次累计反向读数`, visible: true },
        // { key: 11, label: `上期冻结日的总流量`, visible: true },
        // { key: 12, label: `本期冻结日的总流量`, visible: true },
        // { key: 13, label: `本次正向日用量`, visible: true },
        // { key: 14, label: `上期正向日用量`, visible: true },
        // { key: 15, label: `上次反向日用量`, visible: true },
        // { key: 16, label: `本次反向日用量`, visible: true },
        // { key: 17, label: `本次净用量`, visible: true },
        // { key: 18, label: `上次净用量`, visible: true },
        // { key: 19, label: `累计读数增长率`, visible: true },
        // { key: 20, label: `反向累计读数增长率`, visible: true },
        // { key: 21, label: `累计读数增长率`, visible: true },
        // { key: 22, label: `用量增长比率`, visible: true },
        // { key: 23, label: `反向用量增长比率`, visible: true },
        // { key: 24, label: `用量增长比率`, visible: false },
        // { key: 25, label: `创建时间`, visible: true },
        // { key: 26, label: `上次冻结时间`, visible: true },
        // { key: 27, label: `本次冻结时间`, visible: true },
        // { key: 28, label: `冻结月份`, visible: true },
        // { key: 29, label: `上次月冻结的月份`, visible: true },
        // { key: 30, label: `冻结月份`, visible: true },
      ],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // AreaName:'',
      // 区域树选项
      areaOptions: [],
      // 区域名称
      keyWordName: undefined,
      // 树状态
      treeVisibale: true,
      // 遮罩层
      loading: true,
      treeloading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大用户表月用量数据表格数据
      terminalsmonfreezedbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        tenantId: null,
        UserID: null,
        TerminalID: null,
        TerminalAddress: null,
        FeezeTime: moment(new Date(new Date().getFullYear(), new Date().getMonth() - 1, new Date().getDate())).format('YYYY-MM')
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // tenantId: [
        //   { required: true, message: "设备归属水司不能为空", trigger: "blur" }
        // ],
        // UserID: [
        //   { required: true, message: "用户主键不能为空", trigger: "blur" }
        // ],
        TerminalID: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAreaTree();
  },
  mounted() {
    // 通过最大显示个数控制展开/折叠
    if (this.maxShow > 0) {
      this.minShowControl()
    }
  },
  watch: {
    // 根据名称筛选区域树
    keyWordName(val) {
      this.$refs.tree.filter(val);
    },
    // AreaName(val){
    //   this.$refs.areatree.filter(val)
    // }
  },
  methods: {
    // 初始化表格 解决表格固定操作列的问题
    init(){
      // 调用接口获取到数据后
      let _self = this
      this.$nextTick(() => {
        window.setTimeout(() => {
          _self.$refs.table.doLayout()
        }, 100)
      })
    },
    // 展开折叠按钮
    shiftCollapseAble() {
      this.fold = !this.fold
      this.minShowControl()
    },
    // 通过maxShow控制元素显示/折叠
    minShowControl() {
      const group = window.document.querySelectorAll(`#searchFilter .el-form-item.el-form-item--${this.size}`)
      const len = group?.length ? group?.length - 1 : 0
      if (this.maxShow < len) {
        group.forEach((item, index) => {
          if (index > this.maxShow - 1 && index < len) {
            item.hidden = !this.fold
          }
        })
        this.collapseAble = true
      } else {
        this.collapseAble = false
      }
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.AreaCode = data.id;
      this.handleQuery();
    },
    // 单击下拉框中的数据
    handleSelectTree(data){
      this.selectOptions.label = data.label
      this.selectOptions.value = data.id
      this.form.areaCode = data.id
    },
    /** 查询区域下拉树结构 */
    getAreaTree() {
      this.treeloading = true
      let list_ = JSON.parse(localStorage.getItem('list'))
      list_.forEach((item) => {
        var obj = {
          id: item.areaCode,
          label: item.areaName,
          children: item.children && item.children.length > 0 ? this.copyTree(item.children) : item.children
        }
        this.areaOptions.push(obj)
      })
      this.treeloading = false
    },
    copyTree(list) {
      list.forEach((item) => {
        item.id = item.areaCode;
        item.label = item.areaName;
        item.children = item.children;
        if (item.children && item.children.length) {
          this.copyTree(item.children);
        }
      });
      return list;
    },
    // 改变tree的显示与隐藏
    changeTreeShow(){
      this.treeVisibale =!this.treeVisibale
    },
    /** 查询大用户表月用量数据列表 */
    getList() {
      this.loading = true;
      this.init()
      listTerminalsmonfreezedb(this.queryParams).then(response => {
        this.terminalsmonfreezedbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        Id: null,
        tenantId: null,
        UserID: null,
        TerminalID: null,
        locationNb: null,
        TerminalName: null,
        TerminalAddress: null,
        ReadNumberO: null,
        ReadNumberN: null,
        ReverseNumberO: null,
        ReverseNumberN: null,
        TotalFlowO: null,
        TotalFlowN: null,
        ReadDosageN: null,
        ReadDosageO: null,
        ReverseDosageO: null,
        ReverseDosageN: null,
        DosageN: null,
        DosageO: null,
        ReadGrowthRate: null,
        ReverseGrowthRate: null,
        TotalFlowGrowthRate: null,
        ReadDosageGrowthRate: null,
        ReverseDosageGrowthRate: null,
        DosageGrowthRate: null,
        CreateTime: null,
        GetTimeO: null,
        GetTimeN: null,
        FeezeTimeN: null,
        FeezeTimeO: null,
        FeezeTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.AreaCode = null
      this.$refs.tree.setCurrentKey(null);
      this.resetForm("queryForm");
      this.queryParams.FeezeTime = moment(new Date(new Date().getFullYear(), new Date().getMonth() - 1, new Date().getDate())).format('YYYY-MM')
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.Id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大用户表月用量数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const Id = row.Id || this.ids
      getTerminalsmonfreezedb(Id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改大用户表月用量数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.Id != null) {
            updateTerminalsmonfreezedb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTerminalsmonfreezedb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.Id || this.ids;
      this.$modal.confirm('是否确认删除大用户表月用量数据编号为"' + Ids + '"的数据项？').then(function() {
        return delTerminalsmonfreezedb(Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datastatistics/terminalsmonfreezedb/export', {
        ...this.queryParams
      }, `terminalsmonfreezedb_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
    .form_col{
      width: 80%;
      ::v-deep .el-form {
        width: 100%;
      }
      ::v-deep .el-form-item {
        width: 25%;
      }
    }
    .form_query{
      width: 20%;
      text-align: right;
    }
  </style>
